package com.appmagna.sleepaidfan;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import com.android.vending.billing.IInAppBillingService;
import com.appmagna.sleepaidfan.util.IabHelper;
import com.appmagna.sleepaidfan.util.IabResult;
import com.appmagna.sleepaidfan.util.Inventory;
import com.appmagna.sleepaidfan.util.Purchase;
import com.crashlytics.android.Crashlytics;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.internal.AnalyticsEvents;
import io.fabric.sdk.android.Fabric;

public class MoreActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "Remove Ads";
    String ITEM_SKU = "removeads";
    Button btnBack;
    ImageButton btnFeedback;
    ImageButton btnRate;
    Button btnRestore;
    ImageButton btnStar1;
    ImageButton btnStar2;
    ImageButton btnStar3;
    ImageButton btnStar4;
    ImageButton btnStar5;
    ImageButton btnUpgrade;
    TextView lblRate;
    AppEventsLogger logger;
    IabHelper mBillingHelper;
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {
            if (result.isSuccess()) {
            }
        }
    };
    /* access modifiers changed from: private */
    public IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (!result.isFailure()) {
                MoreActivity.this.mRemovedAds = inventory.hasPurchase(MoreActivity.this.ITEM_SKU);
                if (MoreActivity.this.mRemovedAds) {
                    MoreActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            MoreActivity.this.doneRemoveAds();
                        }
                    });
                }
            }
        }
    };
    private IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if (purchase != null && result.isSuccess()) {
                Log.e("INAPP", purchase.getSku());
                if (purchase.getSku().equals(MoreActivity.this.ITEM_SKU)) {
                    MoreActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            MoreActivity.this.doneRemoveAds();
                        }
                    });
                }
            }
            if (!result.isSuccess()) {
            }
        }
    };
    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inv) {
            if (!result.isFailure()) {
                MoreActivity.this.mBillingHelper.consumeAsync(inv.getPurchase(MoreActivity.this.ITEM_SKU), MoreActivity.this.mConsumeFinishedListener);
            }
        }
    };
    boolean mRemovedAds = false;
    IInAppBillingService mService;
    ServiceConnection mServiceConn = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName name) {
            MoreActivity.this.mService = null;
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            MoreActivity.this.mService = IInAppBillingService.Stub.asInterface(service);
        }
    };
    int rate;

    /* access modifiers changed from: protected */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
        setContentView(R.layout.activity_more);
        this.logger = AppEventsLogger.newLogger(getApplicationContext());
        InAppBillingSetupModule();
        this.rate = 0;
        this.btnBack = (Button) findViewById(R.id.btnBack);
        this.btnBack.setOnClickListener(this);
        this.btnRestore = (Button) findViewById(R.id.btnRestore);
        this.btnRestore.setOnClickListener(this);
        this.btnRate = (ImageButton) findViewById(R.id.btnRate);
        this.btnRate.setOnClickListener(this);
        this.btnUpgrade = (ImageButton) findViewById(R.id.btnUpgrade);
        this.btnUpgrade.setOnClickListener(this);
        this.btnFeedback = (ImageButton) findViewById(R.id.btnFeedback);
        this.btnFeedback.setOnClickListener(this);
        this.btnStar1 = (ImageButton) findViewById(R.id.btnStar1);
        this.btnStar1.setOnClickListener(this);
        this.btnStar2 = (ImageButton) findViewById(R.id.btnStar2);
        this.btnStar2.setOnClickListener(this);
        this.btnStar3 = (ImageButton) findViewById(R.id.btnStar3);
        this.btnStar3.setOnClickListener(this);
        this.btnStar4 = (ImageButton) findViewById(R.id.btnStar4);
        this.btnStar4.setOnClickListener(this);
        this.btnStar5 = (ImageButton) findViewById(R.id.btnStar5);
        this.btnStar5.setOnClickListener(this);
        this.lblRate = (TextView) findViewById(R.id.lblRate);
        setButtonRate(this.rate);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                onBackPressed();
                return;
            case R.id.btnRestore:
                restore();
                return;
            case R.id.btnStar1:
                this.rate = 1;
                this.logger.logEvent("1 Star");
                setButtonRate(this.rate);
                return;
            case R.id.btnStar2:
                this.rate = 2;
                this.logger.logEvent("2 Star");
                setButtonRate(this.rate);
                return;
            case R.id.btnStar3:
                this.rate = 3;
                this.logger.logEvent("3 Star");
                setButtonRate(this.rate);
                return;
            case R.id.btnStar4:
                this.rate = 4;
                this.logger.logEvent("4 Star");
                setButtonRate(this.rate);
                return;
            case R.id.btnStar5:
                this.rate = 5;
                this.logger.logEvent("5 Star");
                setButtonRate(this.rate);
                return;
            case R.id.btnRate:
                if (this.rate == 1 || this.rate == 2 || this.rate == 3) {
                    this.logger.logEvent("Contact?");
                    showEmailIntent();
                    return;
                }
                this.logger.logEvent("Rate");
                Intent goToMarket = new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + getPackageName()));
                goToMarket.addFlags(1208483840);
                try {
                    startActivity(goToMarket);
                    return;
                } catch (ActivityNotFoundException e) {
                    startActivity(new Intent("android.intent.action.VIEW", Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
                    return;
                }
            case R.id.btnUpgrade:
                this.logger.logEvent("Upgrade Button on More Page (Remove Ads)");
                removeADS();
                return;
            case R.id.btnFeedback:
                this.logger.logEvent("Feedback");
                showEmailIntent();
                return;
            default:
                return;
        }
    }

    public void setButtonRate(int rate2) {
        this.btnStar1.setImageDrawable(getResources().getDrawable(R.drawable.star_normal));
        this.btnStar2.setImageDrawable(getResources().getDrawable(R.drawable.star_normal));
        this.btnStar3.setImageDrawable(getResources().getDrawable(R.drawable.star_normal));
        this.btnStar4.setImageDrawable(getResources().getDrawable(R.drawable.star_normal));
        this.btnStar5.setImageDrawable(getResources().getDrawable(R.drawable.star_normal));
        if (rate2 == 0) {
            this.lblRate.setText("Rate");
        } else if (rate2 == 1) {
            this.btnStar1.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.lblRate.setText("Contact?");
        } else if (rate2 == 2) {
            this.btnStar1.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar2.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.lblRate.setText("Contact?");
        } else if (rate2 == 3) {
            this.btnStar1.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar2.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar3.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.lblRate.setText("Contact?");
        } else if (rate2 == 4) {
            this.btnStar1.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar2.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar3.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar4.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.lblRate.setText("Rate");
        } else if (rate2 == 5) {
            this.btnStar1.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar2.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar3.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar4.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.btnStar5.setImageDrawable(getResources().getDrawable(R.drawable.star_highlight));
            this.lblRate.setText("Rate");
        }
    }

    public void removeADS() {
        initPurchase(this.ITEM_SKU);
    }

    public void restore() {
    }

    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.stay, R.anim.slide_out_down);
    }

    private void initPurchase(final String id) {
        Log.d("INAPP", id);
        this.mBillingHelper = new IabHelper(this, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiSQwBihQ9HGz09VbETZKXu1JDm8aLjbCsW2EVMTU6/i12B/A20+DraXSmiXR7/UXTBTjVsw6M7U1gImeTqR8uaNv88XZmmKMhpQBa6ulW0E3XRImpgyW4+Nha/+frSi/h+EIOe7I00zw8GA6ViRMHC85qK26TjCEd1Idex//yOg4cUBBk+MCH0b62z69pFS4iMg+4ZoTfizku+6/6dAnDDZ3G7kY5edNhlWMtpz53+IjWc/qLj6fv0lB+Yqc8hBup1hZUL8+3fBLvBO9LY+OPgp3kBCKq8XbQ85Ys8y7pr5+fEc7VTbCRaEFT8lBPAL8McVAwcSst6fHCRour1RpSQIDAQAB");
        this.mBillingHelper.enableDebugLogging(true);
        this.mBillingHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                MoreActivity.this.mBillingHelper.queryInventoryAsync(MoreActivity.this.mGotInventoryListener);
                if (MoreActivity.this.mBillingHelper != null) {
                    MoreActivity.this.mBillingHelper.flagEndAsync();
                }
                MoreActivity.this.purchaseItem(id);
            }
        });
    }

    public void purchaseItem(String id) {
        try {
            this.logger.logEvent("Click Remove Ads Upgrade Button (initiated purchase)");
            if (((PendingIntent) this.mService.getBuyIntent(3, getPackageName(), id, IabHelper.ITEM_TYPE_INAPP, "mytoken").getParcelable(IabHelper.RESPONSE_BUY_INTENT)) != null) {
                this.mBillingHelper.launchPurchaseFlow(this, id, 10001, this.mPurchaseFinishedListener, "mytoken");
            }
        } catch (Exception e) {
            this.logger.logEvent("Canceled Purchase for Remove Ads Upgrade");
            ShowAlertDialog(AnalyticsEvents.PARAMETER_DIALOG_OUTCOME_VALUE_CANCELLED);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.mService != null) {
            unbindService(this.mServiceConn);
        }
        if (this.mBillingHelper != null) {
            this.mBillingHelper.dispose();
        }
        this.mBillingHelper = null;
    }

    /* access modifiers changed from: protected */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!this.mBillingHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void consumeItem() {
        this.mBillingHelper.queryInventoryAsync(this.mReceivedInventoryListener);
    }

    public void InAppBillingSetupModule() {
        try {
            Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
            serviceIntent.setPackage("com.android.vending");
            bindService(serviceIntent, this.mServiceConn, 1);
        } catch (Exception e) {
        }
    }

    public void ShowAlertDialog(String msg) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(getString(R.string.app_name));
        alertDialog.setMessage(msg);
        alertDialog.setButton(-1, "Okay", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
    }

    public void doneRemoveAds() {
        Log.e("doneRemoveAds", "doneRemoveAds");
        this.logger.logEvent("Completed Remove Ads Upgrade Purchase");
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        editor.putBoolean("removeAdPurchased", true);
        editor.commit();
    }

    public void showEmailIntent() {
        Intent emailIntent = new Intent("android.intent.action.SENDTO", Uri.fromParts("mailto", "support@appmagna.com ", (String) null));
        emailIntent.putExtra("android.intent.extra.SUBJECT", getResources().getString(R.string.email_subject));
        emailIntent.putExtra("android.intent.extra.TEXT", getResources().getString(R.string.email_body));
        startActivity(Intent.createChooser(emailIntent, "Send email..."));
    }
}
